<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <div class="contentsize">合同汇总统计</div>
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-form ref="form" :model="form" label-width="80px" :inline="true" size="mini">
                    <el-form-item label="合同编号:">
                        <div style="width: 190px">
                            <el-input v-model="form.htbh" placeholder="请输入合同编号" clearable @keyup.enter.native="onSearch"></el-input>
                        </div>
                    </el-form-item>
                    <el-form-item label="档案编号:">
                        <div style="width: 190px">
                            <el-input v-model="form.dabh" placeholder="请输入档案编号" clearable @keyup.enter.native="onSearch"></el-input>
                        </div>
                    </el-form-item>
                    <el-form-item label="产别类型:">
                        <div style="width: 190px">
                            <el-select v-model="form.cb" placeholder="请选择产别类型" clearable @change="onSearch">
                                <el-option v-for="item in cblist" :key="item.code" :label="item.name"
                                    :value="item.code">
                                </el-option>
                            </el-select>
                        </div>
                    </el-form-item>
                    <el-form-item label="房屋地址:">
                        <div style="width: 190px">
                            <el-input v-model="form.fwdz" placeholder="请输入房屋地址" clearable @keyup.enter.native="onSearch"></el-input>
                        </div>
                    </el-form-item>
                    <el-form-item label="承租单位:">
                        <div style="width: 190px">
                            <el-input v-model="form.czr" placeholder="请输入承租单位" clearable @keyup.enter.native="onSearch"></el-input>
                        </div>
                    </el-form-item>
                    <el-form-item label="开始时间:">
                        <div style="width: 190px">
                            <el-date-picker
                                type="date"
                                placeholder="选择开始时间"
                                v-model="form.startTime"
                                style="width: 100%"
                                value-format="yyyy-MM-dd"
                                @change="onSearch"
                            ></el-date-picker>
                        </div>
                    </el-form-item>
                    <el-form-item label="结束时间:">
                        <div style="width: 190px">
                            <el-date-picker
                                type="date"
                                placeholder="选择结束时间"
                                v-model="form.endTime"
                                style="width: 100%"
                                value-format="yyyy-MM-dd"
                                @change="onSearch"
                            ></el-date-picker>
                        </div>
                    </el-form-item>
                    <el-form-item label="房管员:">
                        <div style="width: 190px">
                            <el-select v-model="form.fgy" placeholder="请选择房管员" clearable @change="onSearch" :disabled="getfgyShow">
                                <el-option v-for="item in fgylist" :key="item.id" :label="item.name" :value="item.id"> </el-option>
                            </el-select>
                        </div>
                    </el-form-item>
                    <!-- <el-form-item label="收租方式:">
                        <div style="width:190px">
                            <el-select v-model="form.zjlb" placeholder="请选择收租方式" clearable @change="onSearch">
                                <el-option v-for="item in jzlblist" :key="item.code" :label="item.name" :value="item.code">
                                </el-option>
                            </el-select>
                        </div>
                    </el-form-item> -->
                    <el-form-item>
                        <el-button type="primary" @click="onSearch" icon="el-icon-search" class="searchcolor">查询</el-button>
                        <el-button @click="reSearch" icon="el-icon-refresh" class="searchcolor">重置</el-button>
                        <el-button type="primary" @click="downloadlist" icon="el-icon-download" class="searchcolor">导出</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <div class="tablestyle">
                <el-table
                    :data="tableData"
                    style="width: 100%"
                    :header-cell-style="tableHeaderColor"
                    :row-style="tablerowColor"
                    :row-class-name="tableRowClassName"
                    :cell-style="tablecellColor"
                    border
                    show-summary
                    sum-text="合计"
                    :summary-method="getSummaries"
                    :max-height="tableHd"
                    ref="tableData"
                >
                    <el-table-column prop="htbh" label="合同编号" align="center" width="160"> </el-table-column>
                    <el-table-column prop="dabh" label="档案编号" align="center" width="240" :show-overflow-tooltip="true">
                    </el-table-column>
                    <el-table-column prop="fwdz" label="房屋地址" align="center" width="280" :show-overflow-tooltip="true">
                    </el-table-column>
                    <el-table-column prop="czr" label="承租单位" align="center" width="280"> </el-table-column>
                    <el-table-column prop="zjdj" :label="type == '2' ? '租金单价' : '月租金'" align="center" width="140"> </el-table-column>
                    <el-table-column prop="receivable" label="应收金额" align="center" width="100"> </el-table-column>
                    <el-table-column prop="sndyszr" label="上年度预收转入" align="center" width="140"> </el-table-column>
                    <el-table-column prop="jmje" label="减免金额" align="center"> </el-table-column>
                    <el-table-column prop="dnsys" label="当年实应收" align="center" width="100"> </el-table-column>
                    <el-table-column prop="dnsyis" label="当年实已收" align="center" width="100"> </el-table-column>
                    <el-table-column prop="jieqje" label="结欠金额" align="center" width="140"> </el-table-column>
                    <el-table-column prop="ysje" label="当前预收" align="center" width="140"> </el-table-column>
                    <el-table-column prop="dqyssh" label="当前预收税后" align="center" width="140"> </el-table-column>
                    <el-table-column prop="dnysjqje" label="当年应收旧欠" align="center" width="140"> </el-table-column>
                    <el-table-column prop="dnssjqje" label="当年实收旧欠" align="center" width="140"> </el-table-column>
                    <el-table-column prop="jqje" label="尚欠" align="center" width="140"> </el-table-column>
                    <el-table-column prop="yjqtje" label="其他金额" align="center" width="140"> </el-table-column>
                    <el-table-column prop="tkje" label="退款金额" align="center" width="140"> </el-table-column>
                    <el-table-column prop="ssjehj" label="实收金额合计" align="center" width="140"> </el-table-column>
                    <el-table-column prop="ssjeshe" label="实收金额税额" align="center" width="120"> </el-table-column>
                    <el-table-column prop="ssjehjsh" label="实收金额税后" align="center" width="120"> </el-table-column>
                </el-table>
            </div>
            <div class="pagination" style="float: right">
                <el-pagination
                    background
                    layout="total,prev, pager, next,jumper"
                    :total="total"
                    :current-page.sync="current"
                    :page-size="pageSize"
                    @current-change="handleCurrentChange"
                >
                </el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
import { dydhztj, projectdelete, exportdydhztj, dydhztjhj, getfgyList } from '@/api/index';
import valueList from '@/components/common/valueList.vue';
export default {
    name: 'cwhzlistone',
    data() {
        return {
            type: this.$store.getters.type,
            getfgyShow: false, //房管员判断
            fgylist: [], //房管员列表
            value1: [],
            zjlblist: valueList.zjlblist, //收租方式
            jzlblist: valueList.jzlblist, //收租方式
            cblist: valueList.cblist,//产别
            total: 0,
            current: 1,
            pageSize: 9,
            form: {
                current: 1, // 第几页 ,
                czr: '', // 承租单位 ,
                dabh: '', // 档案编号 ,
                dnssjqje: '', // 当年实收旧欠 ,
                dnsyis: '', // 当年实已收 ,
                dnsys: '', // 当年实应收 ,
                dnysjqje: '', // 当年应收旧欠 ,
                dqyssh: '', // 当前预收税后 ,
                endTime: '', // 结束时间 ,
                htbh: '', // 合同编号 ,
                jieqje: '', // 结欠金额 ,
                jmje: '', // 减免金额 ,
                jqje: '', // 尚欠 ,
                pageSize: 9, // 每页数量 ,
                receivable: '', // 应收金额 ,
                sndyszr: '', // 上年度预收转入 ,
                ssjehj: '', // 实收金额合计 ,
                ssjehjsh: '', // 实收金额税后 ,
                ssjeshe: '', // 实收金额税额 ,
                startTime: '', // 开始时间 ,
                yjqtje: '', // 其他金额 ,
                ysje: '', // 当前预收 ,
                zjdj: '', // 租金单价
                fgy: '',
                fwdz:'',
                cb:''
            },
            tableData: [],
            multipleSelection: [], //预转已选中列表

            timeRange: [],
            pickerMinDate: null,
            pickerRange: 3600 * 1000 * 24 * 365, // 可选择日期范围（一年）
            pickerOptions: {
                onPick: ({ maxDate, minDate }) => {
                    if (minDate && this.pickerMinDate) {
                        this.pickerMinDate = null;
                    } else if (minDate) {
                        this.pickerMinDate = minDate.getTime();
                    }
                },
                disabledDate: (time) => {
                    if (this.pickerMinDate) {
                        return (
                            time.getTime() > this.pickerMinDate + this.pickerRange || time.getTime() < this.pickerMinDate - this.pickerRange
                        );
                    }
                    return false;
                }
            },
            // 合计对象
            hjje: {
                dnssjqje: '', // 当年实收旧欠 ,
                dnsyis: '', // 当年实已收 ,
                dnsys: '', // 当年实应收 ,
                dnysjqje: '', // 当年应收旧欠 ,
                dqyssh: '', // 当前预收税后 ,
                jieqje: '', // 结欠金额 ,
                jmje: '', // 减免金额 ,
                jqje: '', // 尚欠 ,
                receivable: '', // 应收金额 ,
                sndyszr: '', // 上年度预收转入 ,
                ssjehj: '', // 实收金额合计 ,
                ssjehjsh: '', // 实收金额税后 ,
                ssjeshe: '', // 实收金额税额 ,
                yjqtje: '', // 其他金额 ,
                ysje: '', // 当前预收 ,
                zjdj: '' // 租金单价
            },
            tableHd: ''
        };
    },
    components: {},
    computed: {
        // 判断变色
        getcolorred(htzt) {
            console.log(htzt);
            return (htzt) => {
                if (htzt == '正常') {
                    return { color: '#19BE6B', fontWeight: 700 };
                } else if (htzt == '已经到期') {
                    return { color: '#D9001B', fontWeight: 700 };
                } else if (htzt == '即将到期') {
                    return { color: '#FF9900', fontWeight: 700 };
                } else if (htzt == '未签合同') {
                    return { color: '#409ee1', fontWeight: 700 };
                }
            };
        }
    },
    methods: {
        // 列表样式
        tableHeaderColor({ row, column, rowIndex, columnIndex }) {
            if (rowIndex === 0) {
                return 'background:linear-gradient(to bottom, #008A94,#0ae2f2)!important;color:#ffffff;font-size:14px;';
            }
        },
        tablerowColor({ row, rowIndex }) {
            return 'height: 42px;font-size:14px;color:black';
        },
        tablecellColor() {
            return 'font-weight:700;color:#000000;border-color:#c0c0c0';
        },
        tableRowClassName({ row, rowIndex }) {
            if (rowIndex % 2 != 0) {
                return 'success-row'; //这是类名
            } else {
                return '';
            }
        },
        // 换页
        handleCurrentChange(val) {
            this.current = val;
            this.form.current = val;
            this.dydhztj(this.form);
            console.log(val);
        },
        // 搜索
        onSearch() {
            var that = this;
            this.current = 1;
            this.form.current = 1;
            let a = new Date(this.form.startTime);
            let b = new Date(this.form.endTime);
            if (this.form.startTime) {
                if (this.form.endTime) {
                    if (a >= b) {
                        this.$message.error('开始时间不能大于结束时间！！');
                    } else {
                        that.dydhztj(that.form);
                        that.dydhztjhj(that.form);
                    }
                } else {
                    this.$message.error('请选择结束时间！！');
                }
            } else if (this.form.endTime) {
                this.$message.error('请选择开始时间！！');
            } else {
                that.dydhztj(that.form);
                that.dydhztjhj(that.form);
            }
            // this.current = 1;
            // this.form.current = 1;
            // this.dydhztj(this.form);
            console.log('搜索');
        },
        // 重置
        reSearch() {
            this.current = 1;
            this.form = {
                current: 1, // 第几页 ,
                czr: '', // 承租单位 ,
                dabh: '', // 档案编号 ,
                dnssjqje: '', // 当年实收旧欠 ,
                dnsyis: '', // 当年实已收 ,
                dnsys: '', // 当年实应收 ,
                dnysjqje: '', // 当年应收旧欠 ,
                dqyssh: '', // 当前预收税后 ,
                endTime: '', // 结束时间 ,
                htbh: '', // 合同编号 ,
                jieqje: '', // 结欠金额 ,
                jmje: '', // 减免金额 ,
                jqje: '', // 尚欠 ,
                pageSize: 9, // 每页数量 ,
                receivable: '', // 应收金额 ,
                sndyszr: '', // 上年度预收转入 ,
                ssjehj: '', // 实收金额合计 ,
                ssjehjsh: '', // 实收金额税后 ,
                ssjeshe: '', // 实收金额税额 ,
                startTime: '', // 开始时间 ,
                yjqtje: '', // 其他金额 ,
                ysje: '', // 当前预收 ,
                zjdj: '', // 租金单价
                fgy: this.$store.getters.role == '10011' ? this.$store.getters.userId : '',
                fwdz:'',
                cb:''
            };
            this.getyear();
            this.dydhztj(this.form);
            this.dydhztjhj(this.form);
        },
        // 添加
        addnew() {
            this.$router.push({
                name: 'addproject',
                path: '/addproject'
            });
        },
        // 删除
        deleteClick(e) {
            console.log(e);
            this.$confirm('确定删除此条信息吗？')
                .then(() => {
                    let data = { id: e.id };
                    projectdelete(data).then((res) => {
                        if (res.code == 200) {
                            this.dydhztj(this.form);
                            this.$message.success('删除成功');
                        }
                    });
                })
                .catch(() => {
                    this.$message.success('删除取消');
                });
        },
        // 项目列表
        dydhztj(data) {
            dydhztj(data).then((res) => {
                if (res.code == 200) {
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                } else {
                    this.tableData = [];
                    this.total = 0;
                }
            });
        },
        // 订约单汇总合计
        dydhztjhj(data) {
            dydhztjhj(data).then((res) => {
                if (res.code == 200) {
                    this.hjje = res.data;
                    console.log(res.data);
                } else {
                    this.$message.error = '请联系管理人员！！';
                }
            });
        },
        // 多条预转已点击事件
        handleSelectionChange(val) {
            console.log('val', val);
            this.multipleSelection = val;
        },
        // 获取当前年
        getyear() {
            var date = new Date();
            var year = date.getFullYear();
            this.form.startTime = year + '-01-01';
            this.form.endTime = year + '-12-31';
        },
        // 导出列表
        downloadlist() {
            exportdydhztj(this.form).then((res) => {
                console.log(res);
                const link = document.createElement('a');
                let blob = new Blob([res], {
                    type: 'application/vnd.ms-excel'
                });
                link.style.display = 'none';
                //设置连接
                link.href = URL.createObjectURL(blob);
                link.download = '订约单汇总统计';
                document.body.appendChild(link);
                //模拟点击事件
                link.click();
                console.log(res);
            });
        },
        // 合并计算某一列
        getSummaries(param) {
            const { columns, data } = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (index === 0) {
                    sums[index] = '合计';
                    return;
                }
                // 只对amount这一列进行总计核算。
                if (column.property === 'dnssjqje') {
                    sums[index] = this.hjje.dnssjqje;
                } else if (column.property === 'dnsyis') {
                    sums[index] = this.hjje.dnsyis;
                } else if (column.property === 'dnsys') {
                    sums[index] = this.hjje.dnsys;
                } else if (column.property === 'dnysjqje') {
                    sums[index] = this.hjje.dnysjqje;
                } else if (column.property === 'dqyssh') {
                    sums[index] = this.hjje.dqyssh;
                } else if (column.property === 'jieqje') {
                    sums[index] = this.hjje.jieqje;
                } else if (column.property === 'jmje') {
                    sums[index] = this.hjje.jmje;
                } else if (column.property === 'jqje') {
                    sums[index] = this.hjje.jqje;
                } else if (column.property === 'receivable') {
                    sums[index] = this.hjje.receivable;
                } else if (column.property === 'sndyszr') {
                    sums[index] = this.hjje.sndyszr;
                } else if (column.property === 'ssjehj') {
                    sums[index] = this.hjje.ssjehj;
                } else if (column.property === 'ssjehjsh') {
                    sums[index] = this.hjje.ssjehjsh;
                } else if (column.property === 'ssjeshe') {
                    sums[index] = this.hjje.ssjeshe;
                } else if (column.property === 'yjqtje') {
                    sums[index] = this.hjje.yjqtje;
                } else if (column.property === 'ysje') {
                    sums[index] = this.hjje.ysje;
                } else if (column.property === 'zjdj') {
                    sums[index] = this.hjje.zjdj;
                } else if (column.property === 'tkje') {
                    sums[index] = this.hjje.tkje;
                } else {
                    sums[index] = '---';
                }
            });
            console.log('tableData', this.tableData);
            console.log('sums', sums);
            // this.form.nzj = sums[4]
            return sums;
        },
        // 获取房管员列表
        getfgyList() {
            let data = {};
            getfgyList(data).then((res) => {
                if (res.code == 200) {
                    this.fgylist = res.data;
                    if (this.$store.getters.role == '10011') {
                        console.log(this.$store.getters.userId);
                        this.form.fgy = this.$store.getters.userId;
                        this.getfgyShow = true;
                    } else {
                        this.getfgyShow = false;
                    }
                    this.dydhztj(this.form);
                    this.dydhztjhj(this.form);
                    console.log(res.data);
                } else {
                    this.$message.error = '获取数据失败！！';
                }
            });
        }
    },
    updated() {
        this.$nextTick(() => {
            this.tableHd = document.body.clientHeight + 'px';
            this.$refs.tableData.doLayout();
        });
    },
    mounted() {},
    created() {
        // 获取房管员列表
        this.getfgyList();
        this.getyear();
    }
};
</script>
<style>
/* .el-table { overflow: visible !important; } */
.el-table__footer-wrapper tbody td.el-table__cell {
    font-weight: 600;
    background: rgb(246, 238, 217);
}

.tablestyle > .el-table {
    overflow: auto;
}

.tablestyle > .el-table .el-table__body-wrapper,
.tablestyle > .el-table .el-table__header-wrapper,
.tablestyle > .el-table .el-table__footer-wrapper {
    overflow: visible;
}

.tablestyle > .el-table::after {
    position: relative !important;
}
</style>

<style scoped>
.container {
    /* height: calc(100vh - 260px); */
    overflow: hidden;
}

.crumbs {
    background-color: #fff;
    padding: 20px 20px;
}

.indexTitle {
    margin-bottom: 20px;
}

.contentsize {
    font-size: 18px;
    font-weight: 700;
}

.handle-box {
    overflow: hidden;
}

/* 新增按钮 */
.addcolor {
    margin-bottom: 10px;
}
</style>
<style>
.success-row {
    background-color: #eaf3fb !important;
}
</style>
